package com.auchan.deltoken;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @author Liang Wenjun
 * @Date 2017-05-08 12:05:05
 *
 */
public class AutoFlushToken {

	static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
	
	/*******PROD*******/
	
	static final String DB_URL = "xxxxxxxxxx";
	
	static final String USER_NAME = "xxxxxx";
	
	static final String PASSWORD = "xxxxxx";
	
	/***TEST*********
	static final String DB_URL = "xxxxxxxx";
	
	static final String USER_NAME = "xxxxxx";
	
	static final String PASSWORD = "xxxxxxx";
	*/
	
	private static final String exceptionMsg = "Usage: autoFlushToken.jar [days], eg. autoFlushToken.jar 180";
	
	//
	public int executorSql(String sql){
		Connection conn = null;
		
		PreparedStatement pstmt = null;
		
		int returnFlag = 0;
		try {
			Class.forName(JDBC_DRIVER);
			conn = DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
			pstmt = conn.prepareStatement(sql);
			returnFlag = pstmt.executeUpdate();
			conn.commit();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(pstmt != null){
					pstmt.close();
				}
				if(conn != null){
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return returnFlag;
	}
	
	public static void main(String[] args) {
		int num = 0;
			if(args.length != 1 ){
				System.out.println(exceptionMsg);
				return;
			}else{
				try{
					 num = Integer.parseInt(args[0]);
				}catch (Exception e) {
					System.out.println(exceptionMsg);
					return;
				}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					System.out.println("*************************************************");
					System.out.println("currentTime:"+ sdf.format(new Date()));
					System.out.println("DB connection info :" + DB_URL);
					System.out.println("DB username:"+ USER_NAME);
					System.out.println("delete token begin!");
					inputDayNum(num);
					System.out.println("delete token complete!");
					System.out.println("*************************************************");
					return;
				}
	}
	
	public static void inputDayNum(int input){
		int count = 0;
		String sql = "delete from cas_service_ticket cst where cst.creation_time <"
				+ " ((sysdate - "+input+" - to_date('1970-01-01','yyyy-mm-dd')) * 86400000)";
		
		String sql2 = "delete from cas_ticket_granting_ticket ctgt where ctgt.creation_time < "
				+ "((sysdate - "+input+" - to_date('1970-01-01','yyyy-mm-dd')) * 86400000)";
		//删除ws_log表的数据
		String sql_ws_log = "delete from ws_log wsl where wsl.create_time < sysdate - "+input;
		AutoFlushToken aft = new AutoFlushToken();
		count = aft.executorSql( sql);
		System.out.println("table:cas_service_ticket "+count+" rows is deleted");
		count = aft.executorSql( sql2);
		System.out.println("table:cas_ticket_granting_ticket "+count+" rows is deleted");
		count = aft.executorSql(sql_ws_log);
		System.out.println("table:ws_log "+count+" rows is deleted");
	}
}
